package com.superhelper.superhelper_web.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.superhelper.common.domain.AuserDeliver;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.DadaReasons;
import com.superhelper.common.domain.DeliverRecordStatus;
import com.superhelper.superhelper_web.domain.DeliverRecordOrder;
import com.superhelper.superhelper_web.domain.Order;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.mapper.AdminMapper;
import com.superhelper.superhelper_web.mapper.DeliverMapper;
import com.superhelper.superhelper_web.service.deliver.IDeliverService;

@Service
public class DeliverService {
	protected Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private DeliverMapper deliverMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private OrderService orderService;
	@Autowired
	private IDeliverService dataService;

	public int addSet(AuserDeliver entity) {
		return deliverMapper.addAuserDeviler(entity);
	}

	public int updateDeliverSwitch(Integer auserId, Integer state) {
		return deliverMapper.updateDeliverSwitch(auserId, state);
	}

	public AuserDeliver getAuserDeliver(Integer auserId) {
		return deliverMapper.getAuserDeliver(auserId);
	}

	public List<DeliverRecordOrder> getDeliverRecordList(Integer auserId,
			Integer status, PageModel pageModel) {
		List<AuserShop> shops = adminMapper.getAuserShopsByAuserId(auserId);
		List<Long> shopIds = new ArrayList<Long>();
		for (AuserShop a : shops) {
			shopIds.add(a.getId());
		}
		if (!shops.isEmpty()) {
			int count = deliverMapper.getDeliverRecordListCount(
					StringUtils.join(shopIds, ","), status, pageModel);
			pageModel.setRecordCount(count);
			List<DeliverRecordOrder> list = deliverMapper.getDeliverRecordList(
					StringUtils.join(shopIds, ","), status, pageModel);
			List<Long> orderIds = new ArrayList<Long>();
			for (DeliverRecordOrder d : list) {
				orderIds.add(d.getOrderId());
			}
			if (!orderIds.isEmpty()) {
				List<Order> orders = orderService.getOrderByIds(orderIds);
				for (DeliverRecordOrder s : list) {
					for (Order o : orders) {
						if (s.getOrderId().equals(o.getId())) {
							s.setDaySn(o.getDaySn());
							s.setPhone(o.getPhone());
							s.setConsigness(o.getConsigness());
							s.setTotalPrice(o.getTotalPrice());
							s.setDeliverPoi(o.getDeliverPoi());
							s.setType(o.getType());
							long time = s.getStatusChangeAt().getTime() / 1000;
							long t = System.currentTimeMillis() / 1000;
							if (s.getStatus().equals(
									DeliverRecordStatus.waiting.getVal())) {
								time = s.getDeliverAt().getTime() / 1000;
							} else if (s.getStatus().equals(
									DeliverRecordStatus.abnormal.getVal())) {
								time = s.getDeliverAt().getTime() / 1000;
							} else if (s.getStatus().equals(
									DeliverRecordStatus.completed.getVal())) {
								time = s.getDeliverAt().getTime() / 1000;
								t = s.getStatusChangeAt().getTime() / 1000;
							}

							long pending = (t - time) / 60 > 0 ? (t - time) / 60
									: 1;
							s.setPendMinute((int) pending);
						}
					}
				}
			}
			return list;
		}

		return Collections.emptyList();
	}

	@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
	public int addTips(Long id, Double tips) throws Exception {
		int res = deliverMapper.addTips(id, tips);
		if (res > 0) {
			DeliverRecordOrder record = deliverMapper.getDeliverRecordOrder(id);
			dataService.addTips(record.getId(), record.getCurrentTip());
		}
		return res;
	}

	@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
	public int cancel(Long id, Integer canel_reasonId) throws Exception {
		deliverMapper.deleteTask(String.valueOf(id));
		int res = deliverMapper.cancelRecord(id);
		if (res > 0) {
			dataService.cancel(id, canel_reasonId);
		}
		return res;
	}

	public List<DadaReasons> getReasonsList(Long id) {
		return dataService.getReasonsList(id);
	}
}
